package cn.sdormitory.controller;

import cn.sdormitory.common.api.CommonPage;
import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.violative.entity.ViolationPatrol;
import cn.sdormitory.violative.form.ViolationPatrolGoBackForm;
import cn.sdormitory.violative.form.ViolationPatrolQueryForm;
import cn.sdormitory.violative.form.ViolationPatrolReceiveForm;
import cn.sdormitory.violative.form.ViolationPatrolSaveForm;
import cn.sdormitory.violative.service.ViolationPatrolService;
import cn.sdormitory.violative.vo.ViolationPatrolReceiveInfoVo;
import cn.sdormitory.workflow.entity.ViolationApply;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.jsonwebtoken.lang.Assert;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/app/violation/patrol")
@Api("APP端违纪巡检管理接口")
public class AppViolationPatrolController {

    @Autowired
    private ViolationPatrolService violationPatrolService;

    /**
     * 查询违纪巡查信息
     * @param queryForm 查询参数
     * @return 查询结果
     */
    @PostMapping("/pageList")
    public CommonResult<CommonPage<ViolationPatrol>> pageList(@RequestBody ViolationPatrolQueryForm queryForm){
        IPage<ViolationPatrol> page = violationPatrolService.pageList(queryForm);
        return CommonResult.success(CommonPage.restPage(page));
    }

    @GetMapping("/{id}/getViolationPatrolReceiveInfos")
    public CommonResult<List<ViolationPatrolReceiveInfoVo>> getViolationPatrolReceiveInfos(@PathVariable long id){
        List<ViolationPatrolReceiveInfoVo> classDvs = violationPatrolService.getViolationPatrolReceiveInfos(id);
        return CommonResult.success(classDvs);
    }

    /**
     * 获取违纪巡查详情
     * @param id 违纪id
     * @return 违纪信息
     */
    @GetMapping("/getDetail/{id}")
    public CommonResult<ViolationPatrol> getDetail(@PathVariable long id){
        ViolationPatrol detail = violationPatrolService.getDetail(id);
        Assert.notNull(detail,"获取详情失败");
        return CommonResult.success(detail);
    }

    @PostMapping("/save")
    public CommonResult<Boolean> saveViolationPatrol(@RequestBody ViolationPatrolSaveForm saveForm){
        violationPatrolService.save(saveForm);
        return CommonResult.success(true);
    }

    @PostMapping("/submit/{id}")
    public CommonResult<Boolean> submitViolationPatrol(@PathVariable long id){
        violationPatrolService.submit(id);
        return CommonResult.success(true);
    }

    @DeleteMapping("/{id}")
    public CommonResult<Boolean> deleteViolationPatrol(@PathVariable long id){
        violationPatrolService.delete(id);
        return CommonResult.success(true);
    }

    @PostMapping("/students/receive")
    public CommonResult<Boolean> receiveViolationPatrol(@RequestBody ViolationPatrolReceiveForm receiveForm){
        String errorMsg = violationPatrolService.receive(receiveForm);
        return errorMsg == null ? CommonResult.success(true) : CommonResult.failed(errorMsg);
    }

    @PostMapping("/student/goBack")
    public CommonResult<Boolean> goBackViolationPatrol(@RequestBody ViolationPatrolGoBackForm goBackForm){
        String errorMsg = violationPatrolService.goBackViolationPatrol(goBackForm);
        return errorMsg == null ? CommonResult.success(true) : CommonResult.failed(errorMsg);
    }
    
    @ApiOperation(value = "违纪统计")
    @PostMapping("/getViolationPatrolStatistics")
    public CommonResult getViolationPatrolStatistics(@RequestBody Map<String, String> param){
        String startDate = param.get("startDate");
        String endDate = param.get("endDate");
        List<Map<String, String>> resultList = violationPatrolService.getViolationPatrolStatistics(startDate, endDate);
        return CommonResult.success(resultList);
    }
    
    @ApiOperation(value = "巡查违纪详情")
    @PostMapping("/getViolationPatrolStatisticsDetail")
    public CommonResult getViolationPatrolStatisticsDetail(@RequestBody Map<String, String> param){
        String startDate = param.get("startDate");
        String endDate = param.get("endDate");
        String classId = param.get("classId");
        List<ViolationPatrol> resultList = violationPatrolService.getViolationPatrolStatisticsDetail(startDate, endDate, classId);
        return CommonResult.success(resultList);
    }

}
